package net.mihome.app.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;

import net.mihome.app.AppContext;
import net.mihome.app.R;
import net.mihome.app.bean.Shop;
import net.mihome.app.bean.URLs;
import net.mihome.app.bean.net.ResponsePojo;
import net.mihome.app.common.BitmapManager;
import net.mihome.app.utils.MiAsyncHttpResponseHandler;
import net.mihome.app.utils.RequestParamsFactory;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ListViewDianpuAdapter extends ArrayAdapter<Shop> {

    private static final String TAG = "SampleAdapter";
    private LayoutInflater listContainer;
    private List<Shop> listItems=new ArrayList<Shop>();
    private BitmapManager bmpManager;
    private Context context;

    public ListViewDianpuAdapter(Context context, int resource, List<Shop> objects) {
        super(context, resource, objects);
        listContainer = LayoutInflater.from(context);
        listItems=objects;
        bmpManager=new BitmapManager(BitmapFactory.decodeResource(context.getResources(), R.drawable.tweet_confirm_dialog_load));
        // TODO Auto-generated constructor stub
    }
    public ListViewDianpuAdapter(final Context context, final int textViewResourceId) {
        super(context, textViewResourceId);
        listContainer = LayoutInflater.from(context);
    }

    @Override
    public View getView(final int position, View v, final ViewGroup parent) {
        if(v==null){
            v = listContainer.inflate(R.layout.mifuwu_dianpu_listview_item, parent, false);
        }
        ImageView ivImageView=(ImageView) v.findViewById(R.id.dianpu_avatar);
        if(listItems.get(position).getShopType()==null||"".equals(listItems.get(position).getShopType().getAvatar())){
            ivImageView.setImageResource(R.drawable.icon);
        }else{
              ImageLoader.getInstance().displayImage(listItems.get(position).getShopType().getAvatar(),ivImageView);
        }
        TextView textView=(TextView) v.findViewById(R.id.dianpu_name);
        textView.setText(listItems.get(position).getName());
        TextView dianpuDesc= (TextView) v.findViewById(R.id.dianpu_desc);
        TextView supportTextView= (TextView) v.findViewById(R.id.support_num);
        supportTextView.setText(listItems.get(position).getSupport()+"");
        TextView trampTextView= (TextView) v.findViewById(R.id.tramp_num);
        trampTextView.setText(listItems.get(position).getTramp()+"");
        View supportLayout=v.findViewById(R.id.support_layout);
        View trampLayout=v.findViewById(R.id.tramp_layout);
        supportLayout.setTag(listItems.get(position));
        trampLayout.setTag(listItems.get(position));
        addSupportLinster(supportLayout,supportTextView);
        addTrampLinster(trampLayout,trampTextView);
        dianpuDesc.setText(listItems.get(position).getDesc());
        return v;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listItems.size();
    }
    @Override
    public Shop getItem(int position) {
        // TODO Auto-generated method stub
        return listItems.get(position);
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return listItems.get(position).getId();
    }
    public void addSupportLinster(View view ,final TextView textView){
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Shop shop= (Shop) view.getTag();
                RequestParams requestParams= RequestParamsFactory.newInstance(AppContext.getInstance());
                requestParams.add("shop_id",shop.getId()+"");
                String intString=textView.getText().toString();
                int num=Integer.parseInt(intString);
                num++;
                textView.setText(num+"");
                AppContext.ASYNC_HTTP_CLIENT.get(context, URLs.MIHOME_GET_SHOP_SUPPORT,requestParams,new MiAsyncHttpResponseHandler<Map>() {
                    @Override
                    public void onSuccess(int arg0, Header[] arg1, ResponsePojo<Map> responsePojo) {

                    }

                    @Override
                    public void onFailure(int arg0, Header[] arg1, Throwable arg3) {

                    }
                });
            }
        });
    }
    public void addTrampLinster(View view,final TextView textView){
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Shop shop= (Shop) view.getTag();
                RequestParams requestParams= RequestParamsFactory.newInstance(AppContext.getInstance());
                requestParams.add("shop_id",shop.getId()+"");
                String intString=textView.getText().toString();
                int num=Integer.parseInt(intString);
                num++;
                textView.setText(num+"");
                AppContext.ASYNC_HTTP_CLIENT.get(context, URLs.MIHOME_GET_SHOP_TRAMPLE,requestParams,new MiAsyncHttpResponseHandler<Map>() {
                    @Override
                    public void onSuccess(int arg0, Header[] arg1, ResponsePojo<Map> responsePojo) {

                    }

                    @Override
                    public void onFailure(int arg0, Header[] arg1, Throwable arg3) {

                    }
                });
            }
        });
    }
}
